package easier.app.x.result.location;

import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AlertDialog.Builder;
import easier.app.x.CommonCode;

/**
 * {@link LocationPermissionServiceFunction}的实现方式之一,使用alertDialog引导
 */
public class LocationPermissionServiceFunctionDialogImpl extends LocationPermissionServiceFunction {

    public LocationPermissionServiceFunctionDialogImpl( Context context ) {

        super( context );
    }

    @Override
    public void processRequestForPermission( Runnable confirm, Runnable cancel ) {

        AlertDialog dialog = new Builder( mContext )
            .setTitle( "位置信息" )
            .setMessage( "app需要使用一下位置信息，提供周围地理服务" )
            .setNegativeButton( "取消", new OnClickListener() {
                @Override
                public void onClick( DialogInterface dialogInterface, int i ) {

                    if ( cancel != null ) {
                        cancel.run();
                    }
                }
            } )
            .setPositiveButton( "确定", new OnClickListener() {
                @Override
                public void onClick( DialogInterface dialogInterface, int i ) {

                    if ( confirm != null ) {
                        confirm.run();
                    }
                }
            } )
            .create();
        dialog.show();
    }

    @Override
    public void processShouldShowRequestPermissionRationale( Runnable confirm, Runnable cancel ) {

        AlertDialog dialog = new Builder( mContext )
            .setTitle( "位置信息" )
            .setMessage( "请在应用信息权限管理中设置位置信息为始终允许或者使用期间" )
            .setNegativeButton( "取消", new OnClickListener() {
                @Override
                public void onClick( DialogInterface dialogInterface, int i ) {

                    if ( cancel != null ) {
                        cancel.run();
                    }
                }
            } )
            .setPositiveButton( "确定", new OnClickListener() {
                @Override
                public void onClick( DialogInterface dialogInterface, int i ) {

                    if ( confirm != null ) {
                        confirm.run();
                    }
                }
            } )
            .create();
        dialog.show();
    }

    @Override
    public void processLocationServiceSetting( Runnable confirm, Runnable cancel ) {

        AlertDialog dialog = new Builder( mContext )
            .setTitle( "定位服务" )
            .setMessage( "请在手机设置中打开定位服务" )
            .setNegativeButton( "取消", new OnClickListener() {
                @Override
                public void onClick( DialogInterface dialogInterface, int i ) {

                    if ( cancel != null ) {
                        cancel.run();
                    }
                }
            } )
            .setPositiveButton( "确定", new OnClickListener() {
                @Override
                public void onClick( DialogInterface dialogInterface, int i ) {

                    if ( confirm != null ) {
                        confirm.run();
                    }
                }
            } )
            .create();
        dialog.show();
    }

    /**
     * @param type 1:没有打开定位服务
     */
    public void notifyFailedResult( int type ) {

        if ( type == LOCATION_PERMISSION_DENY ) {
            CommonCode.showToast( mContext, "没有权限" );
        } else if ( type == LOCATION_SERVICE_SETTING_CLOSE ) {
            CommonCode.showToast( mContext, "没有服务" );
        }
    }
}

